﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.ComponentModel;

namespace TDDraw
{
    public class Level : DrawComponent
    {
        public override Texture2D[] Texture { get { return texture; } }
        private Texture2D[] texture;
        public override Rectangle[] DestinationRectangle { get { return destinationRectangle; } }
        private Rectangle[] destinationRectangle;

        TDUpdate.Level level;

        public Level(TDUpdate.Level updateComponent)
        {
            level = updateComponent;

            level.PropertyChanged += new PropertyChangedEventHandler(updateComponent_PropertyChanged);
            visualizeTiles();
        }

        void updateComponent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Tiles":
                    visualizeTiles();
                    break;
                case "SelectedTile":
                    visualizeSelectedTile();
                    break;
                default:
                    break;
            }
        }

        void visualizeTiles()
        {
            texture = new Texture2D[level.Tiles.Length + 1];
            destinationRectangle = new Rectangle[level.Tiles.Length + 1];

            // [0] ist die Darstellung der "SelectedTile"
            visualizeSelectedTile();

            for (int x = 0; x < level.Tiles.GetLength(0); x++)
            {
                for (int y = 0; y < level.Tiles.GetLength(1); y++)
                {
                    DestinationRectangle[x * level.Tiles.GetLength(0) + y + 1] = new Rectangle(x * DrawManager.TileWidth, y * DrawManager.TileHeight, DrawManager.TileWidth - 1, DrawManager.TileHeight - 1);
                    switch (level.Tiles[y, x])
                    {
                        case TDUpdate.TileEnum.Lawn:
                            Texture[x * level.Tiles.GetLength(0) + y + 1] = TextureManager.Instance.Tile_Lawn;
                            break;
                        case TDUpdate.TileEnum.Water:
                            Texture[x * level.Tiles.GetLength(0) + y + 1] = TextureManager.Instance.Tile_Water;
                            break;
                        case TDUpdate.TileEnum.Lane:
                            Texture[x * level.Tiles.GetLength(0) + y + 1] = TextureManager.Instance.Tile_Lane;
                            break;
                        default:
                            throw new ArgumentException("Found a unknown Tile", "Tiles");
                    }
                }
            }
        }

        void visualizeSelectedTile()
        {
            //TODO: Change Color / draw a real Rectangle
            Texture[0] = TextureManager.Instance.Tile_Water;
            if (level.SelectedTile != null)
            {
                DestinationRectangle[0] = new Rectangle(level.SelectedTile.X * DrawManager.TileWidth - 1, level.SelectedTile.Y * DrawManager.TileHeight - 1, DrawManager.TileWidth + 1, DrawManager.TileHeight + 1);
            }
            else
            {
                DestinationRectangle[0] = new Rectangle(0, 0, 0, 0);
            }
        }

        public override object GetInnerObject(int i)
        {
            if (i == 0)
            {
                // Man kann das AuswahlRechteck (das letzte zu zeichnende Objekte) nicht anklicken, darum wird auch kein Inneres Objekt geworfen.
                return null;
            }
            else
            {
                i--; // Wegen dem AuswahlRechteck an Position 0
                return new Point(i / level.Tiles.GetLength(0), i % level.Tiles.GetLength(0));
            }
        }
    }
}
